package com.augurit.common.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.OmOrg;
import com.augurit.common.entity.OmPosition;
import com.augurit.common.system.convert.OmPositionConverter;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.system.web.org.form.OmPositionForm;
import com.augurit.common.system.web.org.form.OmUserForm;

/**
 * 岗位DAO类
 */
@Repository
public class OmPositionDao extends BaseDao<OmPosition, Long> {
	
	/**
	 * 根据岗位主键获取岗位form对象
	 * @param id 岗位主键
	 * @return 岗位form对象
	 * */
	public OmPositionForm getForm(Long id){
		OmPositionForm form = null;
		
		if(id != null){
			OmPosition entity = this.get(id);
			if(entity != null)
				form = OmPositionConverter.convertToForm(entity);
		}

		return form;
	}
	
	/**
	 * 获取指定岗位的父岗位名称
	 * @param id
	 * @return
	 */
	public String getParentPosName(Long id){
		if(id != null){
			OmPosition entity = this.get(id);
			if(entity != null && entity.getParentPosId() != null){
				OmPosition parent = this.get(entity.getParentPosId());
				if(parent != null)
					return parent.getPosName();
			}
		}
		return null;
	}
	
	/**
	 * 保存岗位Form对象
	 * @param form 岗位Form对象
	 */
	public void save(OmPositionForm form){
		
		if(form != null){
			OmPosition entity = null;
			
			//准备VO对象
			if(form != null && form.getPosId() != null){
				entity = this.get(form.getPosId());
			}else{
				entity = new OmPosition();
			}
			
			//属性值转换
			OmPositionConverter.convertToEntity(form, entity);
			
			//保存
			super.save(entity);
			
			//回填ID属性值
			form.setPosId(entity.getPosId());
		}
	}
	
	/**
	 * 将岗位主键集合 转换成对应的岗位集合
	 * @param posIds 岗位主键集合
	 * @return 对应的岗位集合
	 * */
	public List<OmPositionForm> getForms(List<Long> posIds){
		if(posIds != null && posIds.size() != 0){
			List<OmPosition> list = new ArrayList();
			
			for(Long posId : posIds) {
				OmPosition entity = get(posId);
				list.add(entity);
			}
			return OmPositionConverter.convertToFormList(list);
		}else
			return null;
	}
	
	/**
	 * 根据主键ID查询直属子岗位
	 * @param id 业务字典项对象主键
	 * @return 业务字典项对象主键的集合
	 */
	public List<Long> getChildPositions(Long parentPosId){
		if(parentPosId != null)
			return partSelectByParentPosId(parentPosId, "posId");
		else
			return null;
	}
	
	/**
	 * 根据主键ID查询直属子岗位
	 * @param parentPosId 业务字典项对象主键
	 * @return 业务字典项对象主键的集合
	 */
	public List<OmPositionForm> getChildPositionForms(Long parentPosId){
		if(parentPosId != null){
			List<OmPosition> list = partSelectByParentPosId(parentPosId, null);
			return OmPositionConverter.convertToFormList(list);
		}else
			return null;
	}
	
	/**
     * 返回要查询的结果
     * @param funcId
     * @param entityProperty
     * @return
     */
    private List partSelectByParentPosId(Long parentPosId, String entityProperty){
		String hql = "select " 
						+ (entityProperty != null && entityProperty.trim().length() > 0 ? "ps." + entityProperty : "ps") 
						+ " from OmPosition ps where ps.parentPosId = ?";
		List values = new ArrayList();
		values.add(parentPosId);
		
		return this.find(hql,values);
	}
	
	/**
	 * 根据组织类型和组织ID查询直属下级岗位
	 * @param posType 岗位类型
	 * @param partyId 组织id
	 * @return 机构ID查询下级岗位对象主键的集合
	 */
	public List<Long> getPositionIds(String posType, Long partyId){
		if(partyId != null)
			return partSelectByPartyId(posType, partyId, "posId", true);
		else
			return null;
	}
	
	/**
	 * 根据组织类型和组织ID查询下级岗位,包括间接岗位
	 * @param posType 岗位类型
	 * @param partyId 组织id
	 * @return 机构ID查询下级岗位对象主键的集合
	 */
	public List<Long> getAllPositionIds(String posType, Long partyId){
		if(partyId != null)
			return partSelectByPartyId(posType, partyId, "posId", false);
		else
			return null;
	}
	
	
	
	/**
	 * 根据组织类型和组织ID查询直属下级岗位
	 * @param posType 岗位类型
	 * @param partyId 组织id
	 * @return 机构ID查询下级岗位对象主键的集合
	 */
	public List<OmPositionForm> getPositions(String posType, Long partyId){
		if(partyId != null){
			List<OmPosition> list = partSelectByPartyId(posType, partyId, null, true);
			return OmPositionConverter.convertToFormList(list);
		}else
			return null;
	}
	
	/**
	 * 根据组织类型和组织ID查询所有下级岗位
	 * @param posType 岗位类型
	 * @param partyId 组织id
	 * @return 机构ID查询下级岗位对象主键的集合
	 */
	public List<OmPositionForm> getAllPositions(String posType, Long partyId){
		if(partyId != null){
			List<OmPosition> list = partSelectByPartyId(posType, partyId, null, false);
			return OmPositionConverter.convertToFormList(list);
		}else
			return null;
	}
	
	/**
     * 返回要查询的结果
     * @param posType 岗位类型
     * @param partyId 组织Id
     * @param entityProperty
     * @return
     */
    private List partSelectByPartyId(String posType, Long partyId, String entityProperty, boolean isDirect){
		String hql = "select " 
						+ (entityProperty != null && entityProperty.trim().length() > 0 ? "ps." + entityProperty : "ps") 
						+ " from OmPosition ps where ps.posType = ? and ps.partyId = ? " + (isDirect ? " and ps.parentPosId is null" : "");
		List values = new ArrayList();
		values.add(posType);
		values.add(partyId);
		
		return this.find(hql,values);
	}
    
    
    /**
	 * 当指定机构添加了子对象后要对该机构的相关计算字段进行更新
	 * @param orgId 机构主键ID
	 */
	public void updatePositionAfterAddChildren(Long posId){
		if(posId != null){
			OmPosition entity = this.get(posId);
			updatePositionAfterAddChildren(entity);
		}
	}
	
	/**
	 * 当指定模块添加了子对象后要对该机构的相关计算字段进行更新
	 * @param org 机构Entity对象
	 */
	public void updatePositionAfterAddChildren(OmPosition entity){
		if(entity != null){
			int subCount = entity.getSubCount() == null ? 0 : entity.getSubCount() + 1;	//子节点数加1
			entity.setSubCount(subCount);
			entity.setIsLeaf(OrgConstant.ZERO);	//设置为非叶子节点
			this.save(entity);
		}
	}
	
	/**
	 * 当指定机构删除了子对象后要对该机构的相关计算字段进行更新
	 * @param orgId 机构主键ID
	 */
	public void updatePositionAfterRemoveChildren(Long posId){
		if(posId != null){
			OmPosition entity = this.get(posId);
			updatePositionAfterRemoveChildren(entity);
		}
	}
	
	/**
	 * 当指定模块删除了子模块或功能后要对该机构的相关计算字段进行更新
	 * @param org 机构Entity对象
	 */
	public void updatePositionAfterRemoveChildren(OmPosition entity){
		if(entity != null){
			int subCount = entity.getSubCount() - 1;	//子节点数减1
			
			entity.setSubCount(subCount);
			if(subCount == 0)
				entity.setIsLeaf(OrgConstant.ONE);		//设置为叶子节点
			else
				entity.setIsLeaf(OrgConstant.ZERO);		//设置为非叶子节点
				
			this.save(entity);
		}
	}
	
	/**
	 * 根据主键ID通过序列posSeq模糊查询所有子岗位
	 * @param parentPosId 业务字典项对象主键
	 * @return 业务字典项对象主键的集合
	 */
	public List<OmPositionForm> getAllChildPositionForms(Long parentPosId){
		if(parentPosId != null){
			
			String hql = "from OmPosition ps where ps.posSeq like ? and ps.posId != ?";
			List values = new ArrayList();
			values.add("%" + parentPosId + "%");
			values.add(parentPosId);
			
			List<OmPosition> list = this.find(hql,values);
			return OmPositionConverter.convertToFormList(list);
		}
		return null;
	}
	
	/**
	 * 移动岗位到一机构后，修改该岗位以及所有的子岗位的orgId
	 * @param targetOrgId  岗位所要移动到的目标机构
	 * @param list 岗位的所有子岗位，包括本岗位
	 * */
	public void updateOrgIdAfterMovePosition(Long targetOrgId,List<OmPositionForm> list){
		if(targetOrgId != null && list != null && list.size() > 0){
			for (OmPositionForm form : list) {
				form.setPartyId(targetOrgId);
				save(form);
			}
		}
	}
	
	/**
	 * 新增岗位时，为它设置岗位序列
	 * */
	public String getPosSeqAfterSavePos(OmPositionForm form){
		String posSeq = null;
		if(form != null){
			Long parentPosId = form.getParentPosId();
			
			//有父岗位节点
			if(parentPosId != null){
				OmPosition parentPos = this.get(parentPosId);
				posSeq = parentPos.getPosSeq()+form.getPosId()+".";
			}
			
			//没有父岗位节点
			else{
				posSeq = "."+form.getPosId()+".";
			}
		}
		return posSeq;
	}
	
	/**
	 * 
	 * @param orgList
	 * @param keyword
	 * @return
	 */
	public List<OmPositionForm> getPosByOrgListAndKeyword(List<OmOrgForm>orgList,String keyword){
		List<OmPositionForm> posList = null;
		if(orgList != null && orgList.size()>0 && keyword != null && keyword.trim().length()>0){
			StringBuffer sb = new StringBuffer("from OmPosition ps where ps.posName like '"+keyword +"%' and ps.posType='ORG'");
			sb.append("and (1=2");
			for(OmOrgForm org :orgList){
				sb.append(" or ps.partyId=" + org.getOrgId());
			}
			sb.append(")");
			List<OmPositionForm> entityLisy = this.find(sb.toString());
		}
		return posList;
	}
	
	public List<OmUserForm> getPosBykeyword(Long posId,String keyword){
		List<OmUserForm> list = null;
		if(posId != null && keyword != null && keyword.trim().length()>0){
			List<OmPositionForm> posList = this.getAllChildPositionForms(posId);
			if(posList == null)
				posList = new ArrayList<OmPositionForm>();
			posList.add(this.getForm(posId));
		}
		return list;
	}
	
	/*public boolean hasPosLikeKeyword(String keyword){
		if(keyword != null && keyword.trim().length()>0){
			String hql = "from OmPosition ps where ps.posName like '"+keyword +"%'";
			List<OmPosition> list = this.find(hql);
			if(list != null && list.size()>0)
				return true;
		}
		return false;
	}*/
}
